// WndSearchOpt.cpp : implementation file
//

#include "stdafx.h"
#include "BBViewer.h"
#include "WndSearchOpt.h"


// CWndSearchOpt

int g_nInterval[] = { 78, 78, 78, 78 };

IMPLEMENT_DYNAMIC(CWndSearchOpt, CWnd)

CWndSearchOpt::CWndSearchOpt()
{
	m_bOptTime		= TRUE;

	int i;
	for (i=0;i<MAX_REC_TYPE;i++)
		m_bRecType[i] = TRUE;

	m_pListMgr = NULL;
}

CWndSearchOpt::~CWndSearchOpt()
{
	if ((m_nMode == 0)||(m_nMode == 2))
	{
		m_burshBox.DeleteObject();
		m_penBorder.DeleteObject();
		m_penBox.DeleteObject();
	}
}


BEGIN_MESSAGE_MAP(CWndSearchOpt, CWnd)
	ON_WM_SIZE()
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_SHOWWINDOW()
	ON_MESSAGE(WM_CTL_MESSAGE, OnCtlMessage)
END_MESSAGE_MAP()



// CWndSearchOpt message handlers


int CWndSearchOpt::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	int i, n;
	LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpOpt);
	m_nMode = lpWndPos->nMode;

	LPST_BMP_INFO lpBmpInfo;
	for (i=0;i<MAX_REC_TYPE;i++)
	{
		for (n=0;n<3;n++)
		{
			lpBmpInfo = theApp.BmpInfo.GetAt(biOptNormal_unchecked+(i*3)+n);
			m_chkRecType[i].SetBitmap(n, lpBmpInfo->hbmpImage);
		}
	}

	if ((m_nMode==0)||(m_nMode==2))
	{
		m_penBorder.CreatePen(PS_SOLID, 1, lpWndPos->dwColorLevel[0]);
		m_penBox.CreatePen(PS_SOLID, 1, lpWndPos->dwColorBackground);
		m_burshBox.CreateSolidBrush(lpWndPos->dwColorBackground);

	}

	m_dwTextColorNormal = lpWndPos->dwColorText;
	m_dwTextColorDisabled = lpWndPos->dwColorLevel[1];

//	TRACE(_T("COLOR_SEARCH_OPT_BD=%d, COLOR_GENERAL_BK=%d,COLOR_GENERAL_BK=%d\n"),COLOR_SEARCH_OPT_BD,COLOR_GENERAL_BK,COLOR_GENERAL_BK);
//	TRACE(_T("m_dwTextColorDisabled=%d, m_dwTextColorNormal=%d,COLOR_GRAY_LITE=%d\n"),m_dwTextColorDisabled,m_dwTextColorNormal,COLOR_GRAY_LITE);

	for (i=0;i<MAX_REC_TYPE;i++)
	{
		lpBmpInfo = theApp.BmpInfo.GetAt(biOptNormal_unchecked+(i*3));
		if (!m_chkRecType[i].Create(NULL, NULL, WS_CHILD|WS_VISIBLE, CRect(0, 0, 0, 0), this, lpBmpInfo->uImageID, NULL))
			return -1;
	}
	UpdateSwapControl();
	UpdateControl();

	return 0;
}

void CWndSearchOpt::OnPaint()
{
	CPaintDC dc(this);
	CDC dcMemory;
	CRect rect;

	this->GetClientRect(&rect);
	CBitmap bmp, *pOldBmp;
	CBrush *pOldBursh;
	CPen *pOldPen;
	CFont *pOldFont, font;
	int nGap = 2;

	bmp.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
	dcMemory.CreateCompatibleDC(&dc);

	pOldBmp = dcMemory.SelectObject(&bmp);

	if ((m_nMode == 0)||(m_nMode == 2))
	{
		pOldBursh = dcMemory.SelectObject(&m_burshBox);
		pOldPen = dcMemory.SelectObject(&m_penBox);
		dcMemory.Rectangle(&rect);
		dcMemory.SelectObject(pOldPen);
		dcMemory.SelectObject(pOldBursh);

		if (m_nMode == 0)
		{
			pOldPen = dcMemory.SelectObject(&m_penBorder);
			dcMemory.MoveTo(rect.left+nGap, rect.top+nGap);
			dcMemory.LineTo(rect.right-nGap, rect.top+nGap);
			dcMemory.LineTo(rect.right-nGap, rect.bottom-nGap);
			dcMemory.LineTo(rect.left+nGap, rect.bottom-nGap);
			dcMemory.LineTo(rect.left+nGap, rect.top+nGap);
			dcMemory.SelectObject(pOldPen);
		}
	}
	else if (m_nMode == 1)
	{
		CBitmap *pBmpBg, *pOldBmpBg;
		CDC dcBg;
		LPST_BMP_INFO lpBmpInfo = theApp.BmpInfo.GetAt(biOptBG);
		LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpOptBG);
		CRect rtDiv = lpWndPos->rtDiv;
		CRect rtImg = lpBmpInfo->rtImageRect;

		dcBg.CreateCompatibleDC(&dc);
		pBmpBg = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
		pOldBmpBg = dcBg.SelectObject(pBmpBg);

		dcMemory.BitBlt(0, 0, rtDiv.left, rtDiv.top, &dcBg, 0, 0, SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, 0, rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.top,
							&dcBg, rtDiv.left, 0, rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.top, SRCCOPY);
		dcMemory.BitBlt(rect.Width()-rtDiv.right, 0, rtDiv.right, rtDiv.top,
						&dcBg, rtImg.Width()-rtDiv.right, 0, SRCCOPY);
		dcMemory.StretchBlt(0, rtDiv.top, rtDiv.left, rect.Height()-(rtDiv.top+rtDiv.bottom),
							&dcBg, 0, rtDiv.top, rtDiv.left, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, rtDiv.top,
							rect.Width()-(rtDiv.left+rtDiv.right), rect.Height()-(rtDiv.top+rtDiv.bottom),
							&dcBg, rtDiv.left, rtDiv.top,
							rtImg.Width()-(rtDiv.left+rtDiv.right), rtImg.Height()-(rtDiv.top+rtDiv.bottom),
							SRCCOPY);
		dcMemory.StretchBlt(rect.Width()-rtDiv.right, rtDiv.top, rtDiv.right, rect.Height()-(rtDiv.top+rtDiv.bottom),
							&dcBg, rtImg.Width()-rtDiv.right, rtDiv.top,
							rtDiv.right, rtImg.Height()-(rtDiv.top+rtDiv.bottom),
							SRCCOPY);
		dcMemory.BitBlt(0, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
						&dcBg, 0, rtImg.Height()-rtDiv.bottom, SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, rect.Height()-rtDiv.bottom,
							rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
							&dcBg, rtDiv.left, rtImg.Height()-rtDiv.bottom,
							rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
							SRCCOPY);
		dcMemory.BitBlt(rect.Width()-rtDiv.right, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
						&dcBg, rtImg.Width()-rtDiv.right, rtImg.Height()-rtDiv.bottom, SRCCOPY);


		dcBg.SelectObject(pOldBmpBg);
		dcBg.DeleteDC();
	}

	//////////////////////////////////////////////////////////////////////////
	//write text
	int nLeft, nTop;
	int i;
	LOGFONT lf = g_FontSearchOpt;

	lf.lfHeight = 14;
	lf.lfWeight = FW_NORMAL;

	font.CreateFontIndirect(&lf);
	pOldFont = dcMemory.SelectObject(&font); 
	dcMemory.SetBkMode(TRANSPARENT);

	nLeft = 24;
	nTop = rect.Height()/2-8;
	for (i=0;i<4;i++)
	{
		if (!m_bOptTime && (i==0))
			dcMemory.SetTextColor(m_dwTextColorDisabled);
		else
			dcMemory.SetTextColor(m_dwTextColorNormal);
		dcMemory.TextOut(nLeft, nTop, m_pListMgr->GetRecTypeString(i));
		nLeft += g_nInterval[i];
	}
	dcMemory.SelectObject(pOldFont);
	//////////////////////////////////////////////////////////////////////////

	dc.BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

	dcMemory.SelectObject(pOldBmp);
	dcMemory.DeleteDC();
	bmp.DeleteObject();

}

void CWndSearchOpt::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	m_nDlgWidth = cx;
	m_nDlgHeight = cy;

	int nGap = 25;
	int nWidth, i;
	CRect rect;
	nWidth = cx>500 ? 500 : cx;

	if (m_nMode == 1)
	{
		CPoint ptOpt[6];
		CRgn rgn;
		LPST_WND_POS lpWndPos;
		int x1 = 0;
		lpWndPos = theApp.WndPos.GetAt(wpTabTime);
		x1 = lpWndPos->rtWndPos.Width();
		lpWndPos = theApp.WndPos.GetAt(wpTabEvent);
		x1 += lpWndPos->rtWndPos.Width();

		ptOpt[0].x = 0;
		ptOpt[0].y = 5;
		ptOpt[1].x = x1;
		ptOpt[1].y = 5;
		ptOpt[2].x = x1;
		ptOpt[2].y = 0;
		ptOpt[3].x = cx;
		ptOpt[3].y = 0;
		ptOpt[4].x = cx;
		ptOpt[4].y = cy;
		ptOpt[5].x = 0;
		ptOpt[5].y = cy;

		rgn.CreatePolygonRgn(ptOpt, 6, ALTERNATE);
		this->SetWindowRgn(rgn, TRUE);
		rgn.DeleteObject();
	}

	rect.left = 8;
	rect.right = rect.left + 15;
	rect.top = cy/2-8;
	rect.bottom = rect.top + 15;

	for (i=0;i<MAX_REC_TYPE;i++)
	{
		m_chkRecType[i].MoveWindow(rect);
		rect.left += g_nInterval[i];
		rect.right += g_nInterval[i];
	}
}

void CWndSearchOpt::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CWnd::OnShowWindow(bShow, nStatus);

}

void CWndSearchOpt::SetOptTime()
{
	m_bOptTime = TRUE;
	UpdateSwapControl();
	Invalidate();
}

void CWndSearchOpt::SetOptEvent()
{
	m_bOptTime = FALSE;
	UpdateSwapControl();
	UpdateControl();
	Invalidate();
}

void CWndSearchOpt::UpdateSwapControl()
{
	if (m_bOptTime)
	{
		m_chkRecType[REC_NORMAL].EnableWindow(TRUE);
		m_chkRecType[REC_NORMAL].SetCheck(m_bRecType[REC_NORMAL]);
	}
	else
	{
		m_chkRecType[REC_NORMAL].EnableWindow(FALSE);
		m_chkRecType[REC_NORMAL].SetCheck(FALSE);
	}
	return;
}

void CWndSearchOpt::UpdateControl()
{
	int i;
	for (i=0;i<MAX_REC_TYPE;i++)
	{
		m_chkRecType[i].SetCheck(m_bRecType[i]);
	}
}

long CWndSearchOpt::OnCtlMessage(UINT wParam, LONG lParam)
{
	int nControlID		= wParam;			// Control ID
	int nControlValue	= lParam;

	LPST_BMP_INFO lpBmpInfo;
	int nRecType = -1;
	int i;
	for (i=0;i<MAX_REC_TYPE;i++)
	{
		lpBmpInfo = theApp.BmpInfo.GetAt(biOptNormal_unchecked+(i*3));
		if (nControlID == lpBmpInfo->uImageID)
			nRecType = REC_NORMAL+i;
		if (nRecType>=REC_NORMAL)
			break;
	}

	if (nRecType<0)
		return 0;
	

	m_bRecType[nRecType] = !m_bRecType[nRecType];
	UpdateControl();
	GetParent()->PostMessage(WM_PLB_MESSAGE, wParam, (LPARAM)nRecType);
	return 0;
}

BOOL CWndSearchOpt::PreTranslateMessage(MSG* pMsg)
{
	return CWnd::PreTranslateMessage(pMsg);
}

void CWndSearchOpt::RefreshLang()
{
	Invalidate(TRUE);
}
